<template>
	<view>
		<u-navbar title-color="#717171" :background="{ backgroundColor: '#ffffff' }" :back-text-style="{color: '#fff'}"
		 :title="$t('index.PostArticles')">
			<view class="navbar-right" slot="right">
				<view class="message-box right-item">
					<button style="background-color: #ed4141;color: #fff;margin-right: 20rpx;" size="mini" @click="release">{{$t('pressrelease.release')}}</button>
				</view>
			</view>
		</u-navbar>
		<view class="head_text">
			<u-field v-model="title" :label="$t('pressrelease.title')" :placeholder="$t('pressrelease.Pleasetitle')">
			</u-field>
			<jinEdit ref="edjin" :placeholder="$t('pressrelease.pleaseNews')" @editOk="editOk"></jinEdit>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import jinEdit from '../../components/jin-edit/jin-edit.vue';
	export default {
		components: {
			jinEdit
		},
		data() {
			return {
				content: '', //富文本内容
				title: '', //标题
				imgList: [] //图片列表
			}
		},
		onLoad() {
			//监听图片列表
			const _this = this
			uni.$on('globleEvent', function(data) {
				for (const item of data.dataImg) {
					let file = {}
					file.uri = item;
					file.name = 'pics'
					_this.imgList.push(file)
				}
				console.log(_this.imgList)
			})
		},
		methods: {
			//获取富文本内容 
			async editOk(ress) {
				const _this = this
				let data = {}
				data.content = ress.html
				data.title = this.title
				let imgsrcList=[]
				this.imgList.length>=1?imgsrcList=this.imgList:imgsrcList=[{
					"uri": "",
					"name": "pics"
				}]
				const res = await this.$myUploadFile({
					url: '/dmh/add-article-news',
					header: {},
					formData: data,
					files: imgsrcList
				})
				/* res.onProgressUpdate((upres) => {
					console.log('上传进度' + upres.progress);
					console.log('已经上传的数据长度' + upres.totalBytesSent);
					console.log('预期需要上传的数据总长度' + upres.totalBytesExpectedToSend);
				
					// 测试条件，取消上传任务。
					if (upres.progress > 50) {
						res.abort();
					}
				}); */
				if (res.code == 200) {
					_this.$refs.uToast.show({
						title: this.$i18n.messages[this.$i18n.locale].pressrelease.successfully,
						type: 'success'
					})
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/index/index'
						});
					}, 1000)

				} else {
					_this.$refs.uToast.show({
						title: this.$i18n.messages[this.$i18n.locale].pressrelease.failed,
						type: 'error'
					})
				}
			},
			//发布
			release() {
				this.$refs.edjin.release()
			}
		}
	}
</script>
